package com.zyw.customcalendar.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zyw.customcalendar.R;

/**
 * Created by zyw on 2017/9/6.
 */
public class CalendarLayout extends LinearLayout implements ViewPager.OnPageChangeListener {
    public CalendarLayout(Context context) {
        super(context);
        initLayout(context);
    }

    public CalendarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout(context);
    }

    public CalendarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout(context);
    }

    private void initLayout(Context context){
        LayoutInflater.from(context).inflate(R.layout.calendar_layout, this);
        //this.addView(childView);

        viewPager = (ViewPager) findViewById(R.id.viewFlipper1);

        calendarViews[0] = createCalendarView(getContext(), "false");
        calendarViews[1] = createCalendarView(getContext(), "true");
        calendarViews[2] = createCalendarView(getContext(), "false");

        calendarViews[0].setPreviousCalendar(calendarViews[1].getCalendarDraw()
                .getCalendarOperation().returnSelectCalendar());
        calendarViews[2].setNextCalendar(calendarViews[1].getCalendarDraw()
                .getCalendarOperation().returnSelectCalendar());

        PagerAdapter pagerAdapter = new PagerAdapter() {

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {

                return 10000;
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
            }

            @Override
            public int getItemPosition(Object object) {
                return PagerAdapter.POSITION_NONE;
            }

            @Override
            public CharSequence getPageTitle(int position) {

                return "";//直接用适配器来完成标题的显示，所以从上面可以看到，我们没有使用PagerTitleStrip。当然你可以使用。

            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                if(calendarViews[position%3].getParent() != null){
                    ViewGroup viewGroup = (ViewGroup) calendarViews[position%3].getParent();
                    viewGroup.removeView(calendarViews[position%3]);
                }
                container.addView(calendarViews[position%3]);
                return calendarViews[position%3];
            }

        };
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(4801, false);
        lastIndex = 4801;
        viewPager.setOnPageChangeListener(this);

        //mGestureDetector = new GestureDetector(getContext(), this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int currentIndex = viewPager.getCurrentItem();
        calendarViews[currentIndex%3].autoClickDays(calendarViews[currentIndex%3].
            getCalendarDraw().getCalendarOperation().selectDayOfMonth);
    }

    private TextView mYearAndMonth;
    public void setMonthOfYear(TextView monthOfYaer){
        mYearAndMonth = monthOfYaer;
        int currentIndex = viewPager.getCurrentItem();
        CalendarView calendarView = calendarViews[currentIndex%3];
        mYearAndMonth.setText(calendarView.getCalendarDraw().getCalendarOperation().returnCurrentMonths());
    }

    private ViewPager viewPager;
    private CalendarView[] calendarViews = new CalendarView[3];

    private int lastIndex = 0;
    private void refreshView(int position) {
        for (View view :calendarViews) {
            view.setTag("false");
        }

        if(lastIndex > position){
            int tag = position % 3;
            int previousTag = tag - 1;
            if (previousTag == -1)
                previousTag = 2;

            CalendarView currentView = calendarViews[tag];
            currentView.setTag("true");
            currentView.setPreviousCalendar(calendarViews[previousTag].getCalendarDraw()
                    .getCalendarOperation().returnSelectCalendar());
            currentView.autoClickDays(currentView.getCalendarDraw().getCalendarOperation().selectDayOfMonth);

        }else if(lastIndex < position){
            int tag = position % 3;
            int nextTag = tag + 1;
            if (nextTag == 3)
                nextTag = 0;

            CalendarView currentView = calendarViews[tag];
            currentView.setTag("true");
            currentView.setNextCalendar(calendarViews[nextTag].getCalendarDraw()
                    .getCalendarOperation().returnSelectCalendar());
            currentView.autoClickDays(currentView.getCalendarDraw().getCalendarOperation().selectDayOfMonth);
        }
        lastIndex = position;

        if(mYearAndMonth != null) {
            mYearAndMonth.setText(calendarViews[position % 3].getCalendarDraw()
                    .getCalendarOperation().returnCurrentMonths());
        }

        //������һ�����Ǽ���
        //mHandler.postDelayed(new Runnable() {
        //	@Override
        //	public void run() {

        //}
        //}, 800);
    }

    private void showNextView() {

//        int tag = Integer.parseInt((String) viewFlipper1.getCurrentView().getTag());
//        int nextTag = tag + 1;
//        if (nextTag == 3)
//            nextTag = 0;
//        android.util.Log.i("zyw","nextTag="+nextTag);
//
//        CalendarView currentView = calendarViews[nextTag];
//        currentView.setNextCalendar(calendarViews[tag].getCalendarDraw()
//                .getCalendarOperation().returnSelectCalendar());
//
//        viewFlipper1.setInAnimation(AnimationUtils.loadAnimation(getContext(),
//                R.anim.slide_left_in));
//        viewFlipper1.setOutAnimation(AnimationUtils.loadAnimation(getContext(),
//                R.anim.slide_left_out));
//        viewFlipper1.showNext();
//        //������һ�����Ǽ���
//        //mHandler.postDelayed(new Runnable() {
//        //	@Override
//        //	public void run() {
//
//        //}
//        //}, 800);
    }

    public void showCurrentView(){
        CalendarView currentView = calendarViews[viewPager.getCurrentItem()%3];
        currentView.refreshCalendar();
    }

    private CalendarView createCalendarView(Context context, String tag){
        CalendarView child = new CalendarView(context);
        child.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        child.setTag(tag);
        return child;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        refreshView(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public CusViewPager getViewPager(){
        return (CusViewPager)viewPager;
    }
}
